package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	 public static int  getDayCountByYearMonth(Object year,Object month) {
	        int y = 0;
	        int n = 0;
	       
	        y = Integer.parseInt(year.toString());
	        n =Integer.parseInt(month.toString());
	        switch (n) {
	        case 1:
	        case 3:
	        case 5:
	        case 7:
	        case 8:
	        case 10:
	        case 12:
	           return 31;
	            //对于2月份需要判断是否为闰年
	        case 2:
	            if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) {
	                return 29;
	            } else {
	                return 28;
	            }
	        case 4:
	        case 6:
	        case 9:
	        case 11:
	           return 30;
	        default:
	            return -1;
	        }
	    }
	 
	 public static String getStamp(String date){
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		 try {
			String stamp = sdf.parse(date).getTime()+"";
			stamp = stamp.substring(0, stamp.length()-3);
			return stamp;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	 }
	 
	 
	 public static String getNowStamp(){
		Date d = new Date();
		String str   = d.getTime()+"";
		return str.substring(0, 10);
	 }
	 
	 
	 public static String getStamp(String date,String mo){
		 SimpleDateFormat sdf = new SimpleDateFormat(mo);
		 try {
			String stamp = sdf.parse(date).getTime()+"";
			stamp = stamp.substring(0, stamp.length()-3);
			return stamp;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	 }

	public static String getDateStrByStamp(String infoDate, String string) {
		 SimpleDateFormat sdf = new SimpleDateFormat(string);
		 String str ="";
		 try {
		Date date = new Date(Long.parseLong(infoDate+"000"));
		str=sdf.format(date);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}

	public static String getNow(String mod) {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(mod);
		return sdf.format(d);
	}
//	public static String getNow(String mod,String date) {
//		if(mod.equals("stamp")){
//			
//		}
//			return null;
//		}

	public static String getSgtxtDate(String infoDate) {
		Date date = new Date(Long.parseLong(infoDate+"000"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		return sdf.format(date);
	}
	
	public static String getMonthAddOne(String date,int range){
		String[] split = date.split("-");
		int year=Integer.valueOf(split[1])+range>12?Integer.valueOf(split[0])+1:Integer.valueOf(split[0]);
		int month=Integer.valueOf(split[1]);
		int diff=0;
		if(split[1].equals("01")&&Integer.valueOf(split[2])>=DateUtil.getDayCountByYearMonth(year,2)){
			month=2;
			diff=(31-Integer.valueOf(split[2]))*86400;
		}
		
		int day = DateUtil.getDayCountByYearMonth(year,month);
		String da = DateUtil.getStamp(date);
		String newdate = String.valueOf(Integer.valueOf(da)+diff+day*86400);
		return newdate;
	}
	

}
